package net.ib.mtalk.ui.setting;

import java.util.ArrayList;
import java.util.List;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UserSetting;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mntalk.protocol.Session.GetUserSettingReq;
import net.ib.mntalk.protocol.Session.RemoveMessagesReq;
import net.ib.mntalk.protocol.Session.RemoveViewReq;
import net.ib.mntalk.protocol.Session.SetUserSettingReq;
import net.ib.mntalk.protocol.Session.UnRegisterUserReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.database.DatabaseContract.ViewTable;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.task.GetBuddyInfosTask;
import net.ib.mtalk.network.task.GetUserSettingTask;
import net.ib.mtalk.network.task.RemoveMessagesTask;
import net.ib.mtalk.network.task.RemoveViewTask;
import net.ib.mtalk.network.task.SetUserSettingTask;
import net.ib.mtalk.network.task.UnRegisterUserTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;

public class SettingsController extends UIController 
{
	public SettingsController(Context context, Handler handler) {
		super(context, handler);
	}
	
	public void getAllBuddies()
	{
		GetBuddyInfosReq getBuddyInfosReq = new GetBuddyInfosReq();
		getBuddyInfosReq.setPredicate(new Common.UUIDSlicePredicate().setSliceRange((new Common.UUIDSliceRange().setAscending(true).setMaxCount(GetBuddyInfosTask.GET_BUDDY_MAX_COUNT))));
		getBuddyInfosReq.addProperty(Common.BUDDY_NAME).addProperty(Common.BUDDY_CONTACTS);
//		getBuddyInfosReq.addProperty(Common.BUDDY_SEPARATOR);
		try {
			MTalkLogWriter.d("SettingsController.getAllBuddies()");
			GetBuddyInfosTask task = new GetBuddyInfosTask(mContext, mUiHandler, getBuddyInfosReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "SettingsController.getAllBuddies()", e);
		}
	}
	
	public void removeMsg(long viewUuidMsb, long viewUuidLsb)
	{
		RemoveMessagesReq removeMessagesReq = new RemoveMessagesReq();
		removeMessagesReq.setViewUuid(GpbSupport.makeUuidMsg(viewUuidMsb,viewUuidLsb));
		try {
			MTalkLogWriter.d("SettingsController.removeAllMsg()");
			RemoveMessagesTask task = new RemoveMessagesTask(mContext, mUiHandler, removeMessagesReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "SettingsController.removeAllMsg()", e);
		}
	}
	
	public void removeAllViews(Cursor cursor)
	{
//		RemoveViewReq removeViewReq = new RemoveViewReq();
		List<UUIDMsg> viewUuidList = new ArrayList<UUIDMsg>();
		long msb, lsb;
		for(int i = 0 ; i < cursor.getCount() ; i++)
		{
			msb = cursor.getLong(cursor.getColumnIndexOrThrow(ViewTable.KEY_VIEW_UUID_MSB));
			lsb = cursor.getLong(cursor.getColumnIndexOrThrow(ViewTable.KEY_VIEW_UUID_LSB));
			viewUuidList.add(GpbSupport.makeUuidMsg(msb,lsb));
//			removeViewReq.addViewUuid(GpbSupport.makeUuidMsg(msb,lsb));
			
			cursor.moveToNext();
		}
		cursor.close();
		try {
			MTalkLogWriter.d("SettingsController.removeAllViews()");
			RemoveViewTask task = new RemoveViewTask(mContext, mUiHandler, null, viewUuidList);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "SettingsController.removeAllViews()", e);
		}
	}
	
	public void unRegisterUser()
	{
		UnRegisterUserReq req = new UnRegisterUserReq();
		req.setAccessToken(GlobalPreferences.getMessageFromPreferences(AccessToken.class));
		req.setUserUuid(GlobalPreferences.getUserUUID());
		
		try {
			MTalkLogWriter.d("SettingsController.unRegisterUser()");
			UnRegisterUserTask task = new UnRegisterUserTask(mContext, mUiHandler, req);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "SettingsController.unRegisterUser()", e);
		}
	}
	
	public void setUserSetting(boolean push_allowed, boolean push_view_message, boolean push_view_buddy, boolean push_repeat)
	{
		SetUserSettingReq setUserSettingReq = new SetUserSettingReq();
		
		UserSetting userSetting = new UserSetting();
		userSetting.setPushAllowed(push_allowed);
		userSetting.setPushViewMessage(push_view_message);
		userSetting.setPushViewBuddy(push_view_buddy);
		userSetting.setPushRepeat(push_repeat);
		
		setUserSettingReq.setSetting(userSetting);
		
		SetUserSettingTask task = new SetUserSettingTask(mContext, mUiHandler, setUserSettingReq);
		task.execute();
	}
	
	public void getUserSetting()
	{
		GetUserSettingReq getUserSettingReq = new GetUserSettingReq();
		getUserSettingReq.setUserUuid(GlobalPreferences.getUserUUID());
		
		GetUserSettingTask task = new GetUserSettingTask(mContext, mUiHandler, getUserSettingReq);
		task.execute();
	}
}
